package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.UpdateRepositoryDto;
import com.one.group.model.dto.RepositoryQueryDto;
import com.one.group.model.entity.Repository;
import com.one.group.model.vo.RepositoryListVo;
import com.one.group.result.Result;
import com.one.group.service.RepositoryService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("repository")
@Tag(name = "仓库管理接口")
public class RepositoryController {
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @PostMapping("list")
    @Operation(summary = "查询仓库列表")
    public Result list(@RequestBody RepositoryQueryDto repositoryQueryDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        repositoryQueryDto.setTenantId(tenantId);
//        1.准备分页条件
        PageHelper.startPage(repositoryQueryDto.getPage(),repositoryQueryDto.getLimit());
//        2.链式编程准备查询条件

        List<RepositoryListVo> repositoryListVos = repositoryService.findRepositoryByQueryDto(repositoryQueryDto);

//        3.获取分页信息
        PageInfo<RepositoryListVo> pageInfo =new PageInfo<>(repositoryListVos);
//        4.返回分页数据
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询仓库信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(repositoryService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有仓库信息")
    public Result findAll() {
        return ResultUtil.success(repositoryService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除仓库信息")
    public Result delete(@RequestParam Long id){
        if (repositoryService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加仓库信息")
    public Result add(@RequestBody UpdateRepositoryDto updateRepositoryDto){
        Repository repository = new Repository();
        BeanUtils.copyProperties(updateRepositoryDto,repository);
        if (repositoryService.save(repository)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改仓库信息")
    public Result update(@RequestBody UpdateRepositoryDto updateRepositoryDto){
        Repository repository = new Repository();
        BeanUtils.copyProperties(updateRepositoryDto,repository);
        if (repositoryService.updateById(repository)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @GetMapping("/updateStatus")
    @Operation(summary = "修改状态信息")
    public Result updateStatus(@RequestParam Long id){
        if (repositoryService.updateStatus(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除仓库信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (repositoryService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
